<?php

$action = $_POST['action'];
switch ($action) {
    case "update": update_trip();
        break;
    case "create": create_trip();
        break;
    case "change_status_img": change_status_img();
        break;
    case "load_email": load_email();
        break;
    case "send_mail": send_mail();
        break;
    case "checklogin": login();
        break;
    default :;
}

function login() {
    $username = $_POST["username"];
    $password = $_POST["password"];
    if ($username == "webdulichhoaviet@gmail.com") {
        if ($password == "webdulichhoaviet123") {
            session_start();
            $_SESSION["admin"]="1";
            echo 1;
        } else {
            echo 0;
        }
    } else {
        echo 0;
    }
}

function send_mail(){
	$email_nhan = $_POST["email_nhan"];
	$content_mail = $_POST["content_mail"];
	$from_name = $_POST["from_name"];
	$id = $_POST["id"];
	
	require("class.phpmailer.php");
	$mail = new PHPMailer();
	$SMTP_Host = "mail.dulichhoaviet.com.vn";
	$SMTP_Port = 25;
	$SMTP_UserName = "admin@dulichhoaviet.com.vn";
	$SMTP_Password = "cngahdx457877";
	$from = $SMTP_UserName;
	$fromName = "Quản trị website dulichhoaviet.com.vn";
	$to = $email_nhan;
	// Luu y: $SMTP_UserName = $from
	//******************************************************************************/
	/********* Hay bo comment di - Neu truong hop ban can gui toi nhieu mail khac **************/
	//******************************************************************************/
	// $addressCC = "email01@yahoo.com; email02@yahoo.com;";
	// $mail->AddCC($addressCC);
	//******************************************************************************/
	$mail->IsSMTP();
	$mail->Host     = $SMTP_Host;
	$mail->SMTPAuth = true;

	$mail->Username = $SMTP_UserName;
	$mail->Password = $SMTP_Password;

	$mail->From     = $from;
	$mail->FromName = $fromName;
	$mail->AddAddress($to);
	$mail->AddReplyTo($from, $fromName);

	$mail->WordWrap = 50;
	//$mail->AddAttachment("Duong Dan File Dinh Kem");
	$mail->IsHTML(true);

	$mail->Subject  =  "Trả lời câu hỏi của du khách";
	$mail->Body     =  $content_mail;
	$mail->AltBody  =  "Cám ơn Quý khách đã liên lạc tới quản trị viên tại Du lịch Hoa Việt , khi cần hỗ trợ kỹ thuật Quý khách vui lòng liên hệ (086) 268 6525.
							<br/>
							Trân trọng,<br/>
							Mai Minh Phú";
	$mail->CharSet = "utf-8";

	if(!$mail->Send())
	{
	   echo "Thong bao loi: " . $mail->ErrorInfo;
	}
	else{
		require_once('database.php');
        $connection = new database;
        $sql = "UPDATE contact set Status = '1' where ID = '" . $id . "'";
        $connection->setQuery($sql);

        $result = $connection->query();
        if ($result) {
            echo 1;
        } else {
            echo 0;
        }
	}
}


/* function send_mail() {
    $email_nhan = $_POST["email_nhan"];
    $content_mail = $_POST["content_mail"];
    $from_name = $_POST["from_name"];
    $id = $_POST["id"];
    require_once('class.phpmailer.php');
    $mail = new PHPMailer();
	$SMTP_Host = "mail.dulichhoaviet.com.v";
	$SMTP_Port = 25;
	$SMTP_UserName = "email@dulichhoaviet.com.vn";
	$SMTP_Password = "cngahdx457877";
	$from = $SMTP_UserName;
	$fromName = "Quản trị viên website dulichhoaviet.com.vn";
	$to = $email_nhan;
    $mail->IsHTML(true);
    $mail->IsSMTP();
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = $SMTP_Host; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "webdulichhoaviet@gmail.com"; // GMAIL username
    $mail->Password = "webdulichhoaviet123"; // GMAIL password
    $mail->From = "webdulichhoaviet@gmail.com"; // "name@yourdomain.com";
    //$mail->AddReplyTo = "support@shotdev.com"; // Reply
    $mail->FromName = $from_name;  // set from Name
    $mail->Subject = "Trả lời câu hỏi của du khách?";
    $mail->Body = $content_mail;
    $mail->CharSet = "utf-8";

    $mail->AddAddress($email_nhan, $from_name); // to Address
    //$mail->AddAttachment("shotdev/myfile.zip");
    //$mail->AddAttachment("shotdev/myfile2.zip");
    //$mail->AddCC("member@shotdev.com", "Mr.Member ShotDev"); //CC
    //$mail->AddBCC("member@shotdev.com", "Mr.Member ShotDev"); //CC

    $mail->set('X-Priority', '1'); //Priority 1 = High, 3 = Normal, 5 = low

    $rel = $mail->Send();
    if ($rel) {
        require_once('database.php');
        $connection = new database;
        $sql = "UPDATE contact set Status = '1' where ID = '" . $id . "'";
        $connection->setQuery($sql);

        $result = $connection->query();
        if ($result) {
            echo 1;
        } else {
            echo 0;
        }
    } else {
        echo 0;
    }
} */

function change_status_img() {
    $id = $_POST['id'];
    $status = $_POST['status'];

    require_once('database.php');
    $connection = new database;
    $sql = "UPDATE gallery set Status = '" . $status . "' where ID = '" . $id . "'";
    $connection->setQuery($sql);

    $result = $connection->query();
    if ($result) {
        echo "1";
    } else {
        echo "0";
    }
}

function update_trip() {
    $id = $_POST['id'];
    $status_update = $_POST['status'];
    $tripname = $_POST['tripname'];
    $triptype = $_POST['triptype'];
    $placename = $_POST['placename'];
    $des = $_POST['des'];
    $des_file = $_POST['des_file'];
    $folder_img = $_POST['folder_img'];
    $numberofpeople = $_POST['numberofpeople'];

    require_once('database.php');
    $connection = new database;
    $sql = "UPDATE trip set TripName = '" . $tripname . "', PlaceID = '" . $placename . "', 
    Introduce = '" . $des . "', DesLinkFile = '" . $des_file . "', LinkImage = '" . $folder_img . "', 
    TripTypeID = '" . $triptype . "', NumberOfPeople='" . $numberofpeople . "', Status='" . $status_update . "' WHERE ID='" . $id . "'";
    $connection->setQuery($sql);

    $result = $connection->query();
    if ($result) {
        echo "1";
    } else {
        echo "0";
    }
}

function load_email() {
    echo '<table width="900" border="0" cellspacing="0" class="table_1" style="margin-top:20px">
                                    <tr class="title_list">
                                        <td width="5%">STT</td>
                                        <td width="30%">Tên người gửi</td>
                                        <td width="17%">Email</td>
                                        <td width="28%">Nội dung</td>
                                        <td width="15%">Trạng thái</td>
                                        <td width="5%"></td>
                                    </tr>';
    require_once("database.php");
    $connection = new database;
    $sql = "SELECT * from contact";
    $connection->setQuery($sql);

    $ar = $connection->loadAllRow();
    $connection->disconnect();
    foreach ($ar as $a) {
        $id = $a["ID"];
        $tripname = $a["Name"];
        $placename = $a["Email"];
        $typename = $a["Content"];
        $status = $a["Status"];
        echo "<tr>";
        echo "<td>" . $id . "</td>";
        echo "<td>" . $tripname . "</td>";
        echo "<td>" . $placename . "</td>";
        echo "<td>" . $typename . "</td>";
        if ($status == "1") {
            echo "<td id='trangthai_mail_$id'>Đã trả lời</td>";
        } else {
            echo "<td id ='trangthai_mail_$id'>Chưa trả lời</td>";
        }
        echo "<td align='center' style='text-align:center;
        padding:6px;'>";
        echo "<input type='button' id=" . $id . " name='basic' value='+' class='basic button detail_button' title='Xem chi tiết' onclick = 'return showsendmail(this);'/>";
        echo "</td>";
        echo "</tr>";
        echo "<tr id='area_mail_" . $id . "' style='display:none'>";

        echo "<td></td>";
        echo "<td colspan=5 id='area_mail_send_" . $id . "'>";
        if ($status == "0") {
            echo "<p>Email: <input type='text' value='" . $placename . "' style='width:300px' id = 'email_nhan_" . $id . "'/></p>
            <p>Nội dung mail:
            <input type='hidden' id = 'from_name_email_" . $id . "' value = '" . $tripname . "'>
            </p>
            <p><textarea style = 'width:600px' id = 'content_mail_" . $id . "'>@$tripname \r\n " . $typename . "\r\n ------------------- \r\n </textarea></p>
            <p><input type='button' id='" . $id . "' class='button detail_button' value = 'Gửi mail' onclick='sendmail(this)'/></p>";
        } else {
            echo "Gửi thành công";
        }
        "</td>";

        echo "</tr>";
    }
    echo '</table>';
}

function create_trip() {
    $status = $_POST['status'];
    $tripname = $_POST['tripname'];
    $triptype = $_POST['triptype'];
    $placename = $_POST['placename'];
    $des = $_POST['des'];
    $des_file = $_POST['des_file'];
    $folder_img = $_POST['folder_img'];
    $numberofpeople = $_POST['numberofpeople'];

    require_once('database.php');
    $connection = new database;
    $sql = "insert into trip (TripName,PlaceID,Introduce,DesLinkFile,LinkImage,TripTypeID,NumberOfPeople,Status) values 
('" . $tripname . "',
'" . $placename . "', 
'" . $des . "',
'" . $des_file . "',
'" . $folder_img . "', 
'" . $triptype . "',
'" . $numberofpeople . "',
'" . $status . "')";
    $connection->setQuery($sql);

    $result = $connection->query();
    if ($result) {
        echo "1";
    } else {
        echo "0";
    }
}
?>

